package com.doeis.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 基础过滤器,主要过滤编码
 * 
 * @author xieyan
 * 
 */
@WebFilter(urlPatterns = { "/*" })
public class CommonFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

		// 将ServletRequest对象转换HttpServletRequest
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		// 将ServletRequest对象转换HttpServletResponse
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		String url = httpRequest.getRequestURI();

		httpRequest.setCharacterEncoding("UTF-8");
		// 设置不读取IE缓存
		if (!url.contains("/image")) {
			httpResponse.setHeader("Pragma", "No-Cache");
			httpResponse.setHeader("Cache-Control", "No-Cache");
			httpResponse.setDateHeader("Expires", 0);
		}
		httpRequest.setCharacterEncoding("UTF-8");

		if (httpResponse.isCommitted() == false)
			chain.doFilter(httpRequest, httpResponse);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
